/*!
 Temelia - Binary tree performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/binary_tree.h>
}

static void null_handler(void *key, void *context)
{

}

static binary_tree_t __temelia_binary_tree_insert(int n)
{
	binary_tree_t root, parent, add;
	n /= 100;

	root = parent = binary_tree_new(default_collection[0]);

	for (int i = 0; i < n; i++)
	{
		add = binary_tree_new(default_collection[i]);
		if (i % 2)
			binary_tree_join(parent, NULL, add);
		else
			binary_tree_join(parent, add, NULL);
		parent = add;
	}

	return root;
}

static void temelia_binary_tree_insert(int n)
{
	binary_tree_delete(__temelia_binary_tree_insert(n));
}

static void temelia_binary_tree_inorder(int n)
{
	binary_tree_t root = __temelia_binary_tree_insert(n);
	binary_tree_inorder(root, null_handler, NULL);
	binary_tree_delete(root);
}

void TemeliaPerformance::pt_binary_tree(void)
{
	TestUnit testUnit("binary_tree");

	GENERATE_TEMELIA_TEST(testUnit, "insert", binary_tree_insert);
	GENERATE_TEMELIA_TEST(testUnit, "inorder", binary_tree_inorder);

	testUnit.write(fout);
}
